#!/bin/env bash
#
black="\e[30m"
red="\033[31m"
green="\033[32m"
yellow="\033[33m"
blue="\033[34m"
purple="\033[35m"
cyan="\033[36m"
white="\033[37m"
background="\033[0m"

GithubMirror="https://gh.arcticfox.top"

if [ ! -d AFHOME ]
  mkdir AFHOME
fi
cd AFHOME

function Download(){
file="$1"
URL="$2"
local i=0
if wget --help | grep -q show-progress 
then
  echo -e ${blue}[${green}*${blue}] ${cyan}正在下载 ${yellow}${file}${cyan}${background}
  until wget -q --show-progress -O ${file} -c ${URL}
  do
    if [ ${i} == 3 ]
    then
      echo -e ${blue}[${red}*${blue}] ${cyan}错误次数过多 ${yellow}退出${background}
      return 1
    fi
    i=$((${i}+1))
    echo -e ${blue}[${red}*${blue}] ${cyan}下载失败 ${yellow}三秒后重试${background}
    sleep 3s
  done
  echo -e ${blue}[${green}*${blue}] ${cyan}下载完成.${background}
  return 0
else
  echo -e ${blue}[${green}*${blue}] ${cyan}正在下载 ${yellow}${file}${cyan}${background}
  until wget -O ${file} -c ${URL}
  do
    if [ ${i} == 3 ]
    then
      echo -e ${blue}[${red}*${blue}] ${cyan}错误次数过多 ${yellow}退出${background}
      return 1
    fi
    i=$((i+1))
    echo -e ${blue}[${red}*${blue}] ${cyan}下载失败 ${yellow}三秒后重试${background}
    sleep 3s
  done
  echo -e ${blue}[${green}*${blue}] ${cyan}下载完成.${background}
  return 0
fi
}

echo -e ${green}正在修改 ${yellow}DNS配置${background}
rm /etc/resolv.conf
echo 'nameserver 223.5.5.5
nameserver 223.6.6.6
nameserver 2400:3200::1
nameserver 2400:3200:baba::1' > /etc/resolv.conf
cat /etc/resolv.conf
echo -e ${green}已修改为 ${yellow}阿里DNS${background}

echo -e ${green}正在修改 ${yellow}Pacman仓库源${background}

echo '
##
## MSYS2 repository mirrorlist
##

## Primary
## msys2.org
Server = https://mirrors.bfsu.edu.cn/msys2/msys/$arch
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/$arch
Server = https://mirror.nju.edu.cn/msys2/msys/$arch
Server = https://mirrors.aliyun.com/msys2/msys/$arch
Server = https://mirror.msys2.org/msys/$arch
Server = https://repo.msys2.org/msys/$arch
' > mirrorlist.msys

cat > mirrorlist.mingw64 <<- FOX
##
## 32-bit Mingw-w64 repository mirrorlist
##

## Primary
## msys2.org
Server = https://mirrors.bfsu.edu.cn/msys2/msys/x86_64
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/x86_64
Server = https://mirror.nju.edu.cn/msys2/msys2/msys/x86_64
Server = https://mirrors.aliyun.com/msys2/msys/x86_64
Server = https://mirror.msys2.org/msys/x86_64
Server = https://repo.msys2.org/msys/x86_64
FOX

cat > mirrorlist.mingw32 <<- FOX
##
## 32-bit Mingw-w64 repository mirrorlist
##

## Primary
## msys2.org
Server = https://mirrors.bfsu.edu.cn/msys2/msys/i686
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/i686
Server = https://mirror.nju.edu.cn/msys2/msys/i686
Server = https://mirrors.aliyun.com/msys2/msys/i686
Server = https://mirror.msys2.org/msys/i686
Server = https://repo.msys2.org/msys/i686
FOX

cat > mirrorlist.clang64 <<- FOX
## Primary
Server = https://mirrors.bfsu.edu.cn/msys2/mingw/clang64
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/clang64
Server = https://mirror.nju.edu.cn/msys2/mingw/clang64
Server = https://mirrors.aliyun.com/msys2/mingw/clang64
Server = https://mirror.msys2.org/mingw/clang64
Server = https://repo.msys2.org/mingw/clang64
FOX

cat > mirrorlist.clang32 <<- FOX
## Primary
Server = https://mirrors.bfsu.edu.cn/msys2/mingw/clang32
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/clang32
Server = https://mirror.nju.edu.cn/msys2/mingw/clang32
Server = https://mirrors.aliyun.com/msys2/mingw/clang32
Server = https://mirror.msys2.org/mingw/clang32
Server = https://repo.msys2.org/mingw/clang32
FOX

cat > mirrorlist.ucrt64 <<- FOX
## Primary
Server = https://mirrors.bfsu.edu.cn/msys2/mingw/ucrt64
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/ucrt64
Server = https://mirror.nju.edu.cn/msys2/mingw/ucrt64
Server = https://mirrors.aliyun.com/msys2/mingw/ucrt64
Server = https://mirror.msys2.org/mingw/ucrt64
Server = https://repo.msys2.org/mingw/ucrt64
FOX

mv -f mirrorlist.msys /etc/pacman.d/mirrorlist.msys
mv -f mirrorlist.mingw64 /etc/pacman.d/mirrorlist.mingw64
mv -f mirrorlist.mingw32 /etc/pacman.d/mirrorlist.mingw32
mv -f mirrorlist.clang64 /etc/pacman.d/mirrorlist.clang64
mv -f mirrorlist.clang32 /etc/pacman.d/mirrorlist.clang32
mv -f mirrorlist.ucrt64 /etc/pacman.d/mirrorlist.ucrt64
cat /etc/pacman.d/mirrorlist.msys
echo -e ${green}修改完成${background}

echo -e ${green}正在更新 ${yellow}MSYS2${background}
pacman -Syyu --noconfirm --needed
echo -e ${green}更新完成${background}

echo -e ${green}正在安装 ${yellow}依赖软件${background}
pacman -S --needed --noconfirm tar gzip xz tmux curl wget pv git unzip
echo -e ${green}安装完成${background}
echo
echo -e ${green}开始安装 ${yellow}chromium${background}
ChromiumURL="https://registry.npmmirror.com/-/binary/playwright/builds/chromium/1124/chromium-win64.zip"
Download chromium.zip ${ChromiumURL}
echo -e ${green}正在解压 ${yellow}chromium.zip${background}
if unzip -q chromium.zip
then
  echo -e ${green}解压完成${background}
else
  echo -e ${red}解压失败${background}
  exit
fi
echo
echo -e ${green}开始安装 ${yellow}NodeJS${background}
echo -e ${green}正在获取 ${yellow}NodeJS版本信息${background}
i=0
until NodeVersion="$(curl https://mirrors.ustc.edu.cn/node/latest-iron/ | grep .pkg | awk -F 'node-|\.pkg' '{print $2}')"
do
  if [ ${i} == 3 ]
  then
    echo -e ${cyan}错误次数过多 ${yellow}退出${background}
    exit
  fi
  i=$((${i}+1))
  echo -e ${cyan}获取失败 ${yellow}三秒后重试${background}
  sleep 3s
done

NodeJS_URL="https://registry.npmmirror.com/-/binary/node/latest-iron/node-${NodeVersion}-win-x64.zip"

Download NodeJS.zip ${NodeJS_URL}

echo -e ${green}正在解压 ${yellow}NodeJS.zip${background}
if unzip -q NodeJS.zip -d Node
then
  echo -e ${green}解压完成${background}
  mv Node/$(ls Node) NodeJS
  rm -rf Node
else
  echo -e ${red}解压失败${background}
  exit
fi

echo -e ${green}开始安装 ${yellow}yq${background}
URL="${GithubMirror}/https://github.com/mikefarah/yq/releases/download/v4.44.2/yq_windows_amd64.exe"
Download yq.exe ${URL}
echo -e ${green}安装完成${background}

echo -e ${green}正在安装 ${yellow}ffmpeg${background}
ffmpeg_URL=https://registry.npmmirror.com/-/binary/ffmpeg-static/b6.0
Download ffmpeg.exe ${ffmpeg_URL}/ffmpeg-win32-x64
Download ffprobe.exe ${ffmpeg_URL}/ffprobe-win32-x64
echo -e ${green}安装完成${background}
